



//
//  NHomeViewController.m
//  nncloud
//
//  Created by macfai on 2018/9/17.
//  Copyright © 2018年 youedata. All rights reserved.
#import "ZFScanViewController.h"
#import "PPBadgeView.h"
#import "NMessageViewController.h"
#import "NFeedbackPopView.h"
#import "NHomeViewController.h"
#import "NHomeCell.h"
#import "NHomeSectionHeader.h"
#import "NHomeHeaderView.h"
#import "NProblemContentViewController.h"
#import "NMyFeedbackViewController.h"
#import <MJRefresh/MJRefresh.h>
#import <MJExtension/MJExtension.h>
#import "NFeedbackModel.h"
#import "NFeedbackCell.h"
#import "NWriteMsgViewController.h"
#import "NProcessInstrucitonViewController.h"
#import "MMenu.h"
#import "NNMJRefreshNormalHeader.h"
@interface NHomeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NHomeHeaderView *headerView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger currentPage;

@property (nonatomic,strong)MJRefreshNormalHeader *header;

@end

#define cellIdentifier  @"feedbackid"
#define HeaderHight  290
#define PageSize 20

@implementation NHomeViewController

-(UITableView *)tableView{
    
    
    
    if (!_tableView) {
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - ScreenHeightFlex - kTabHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 85.0f;
//        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NFeedbackCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            [ws loadData];
//        }];

        _tableView.mj_header = [NNMJRefreshNormalHeader headerWithRefreshingBlock:^{
            [ws loadData];
        }];
        
//        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            [ws loadMoreData];
//        }];
//        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if ([NUserModel shareInstance].isLogin) {
        
        if ([[NUserModel shareInstance].badgeCount integerValue]>0) {
            [self.navigationItem.rightBarButtonItem pp_addDotWithColor:[UIColor redColor]];
        }
        
    }else{
        [self.navigationItem.rightBarButtonItem pp_addDotWithColor:nil];
    }
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.view.backgroundColor = kWhiteColor;
    self.navigationItem.leftBarButtonItem = nil;
    [self.navigationItem setItemWithTitle:@"南宁工业云" textColor:kNavigationTitleColor fontSize:18 itemType:center];
     [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 40) title:@"" Image:@"消息中心" selectedIMG:@"消息中心" tartget:self action:@selector(messageAction)];
    
    [self addNavigationLeftButtonFrame:CGRectMake(0, 0, 30, 30) Image:WDImgName(@"") AndHighLightImage:nil AndText:@"" Target:self Action:@selector(scan)];
    
    [self.view addSubview:self.tableView];
    //头视图
    NHomeHeaderView *headView = [[NHomeHeaderView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, HeaderHight)];
    self.headerView = headView;
    self.tableView.tableHeaderView = headView;
    [self blockBannerIndex];
    [self loadData];
    
}


- (void)scan {
    ///扫码
    ZFScanViewController * vc = [[ZFScanViewController alloc] init];
    vc.returnScanBarCodeValue = ^(NSString * barCodeString){
        [AFToast showText:barCodeString];
//        self.resultLabel.text = [NSString stringWithFormat:@"扫描结果:\n%@",barCodeString];
        NSLog(@"扫描结果的字符串======%@",barCodeString);
    };
    
    [self presentViewController:vc animated:YES completion:nil];
    
}
- (void)messageAction
{
    
    WeakStament(ws);
    MMenu *menu = [[MMenu alloc]initCustomImageArray:@[@"企业招聘",@"企业招聘",@"企业招聘",@"企业招聘",] textArray:@[@"营商环境",@"工业运行",@"金融服务",@"大数据",] selfFrame:CGRectMake(ScreenWidth-155, 49, 150, 215)];
    [menu show];
    menu.selectRowBlock = ^(NSString * _Nonnull row) {
        YYLog(@"%@",row);
        
        ws.tabBarController.selectedIndex = 1;
        
    };
    
    //    NMessageViewController *msg = [NMessageViewController new];
//    msg.userType = [NUserModel shareInstance].userInfoType;
//    msg.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:msg animated:YES];
    
}

-(void)loadData {
    self.currentPage = 1;
    [self loadPulicQuestion];
}
//首页加载更多功能去掉，只有五条数据
//- (void)loadMoreData {
//    self.currentPage ++;
//    [self loadPulicQuestion];
//}

- (void)loadPulicQuestion {
    WeakStament(ws);
//    [SVProgressHUD show];
    //新需求要求返回五条进行展示
//    NSDictionary *dic = @{@"size":@(5),@"curPage":@(self.currentPage)};
    [NNetworkClient getRequestWithBaseUrl:kEnterPriseHotQuestionUrl setParameters:nil success:^(id responseObject) {
        YYLog(@"%@",responseObject);
//        YYLog(@"%@",result);
        if ([[responseObject objectForKey:@"code"] integerValue] == 0) {
            NSArray *arr = responseObject[@"page"];
            [ws formData:arr];

            if (arr.count==0) {
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }else{

            [AFToast showText:responseObject[@"msg"]];

        }
    } fail:^(NSError *error) {

        YYLog(@"%@",error);

    } delegater:nil];

    
    //更换成新的数据请求  RC框架相关
//    [[NNRCNetRequest manager] getWithUrl:kEnterPriseHotQuestionUrl params:nil success:^(id result) {
//        YYLog(@"%@",result);
//        if ([[result objectForKey:@"code"] integerValue] == 0) {
//            NSArray *arr = result[@"page"];
//            [ws formData:arr];
//
//            if (arr.count==0) {
//                [self.tableView reloadData];
//                [self.tableView.mj_footer endRefreshingWithNoMoreData];
//            }
//        }else{
//
//            [AFToast showText:result[@"msg"]];
//
//        }
//    } failure:^(id failure) {
//
//        YYLog(@"%@",failure);
//
//    }];
}

- (void)formData:(NSArray *)data {
    
    if (self.currentPage==1) {
        [self.dataArray removeAllObjects];
    }
    for (NSDictionary *dict in data) {
        NFeedbackModel *model = [NFeedbackModel mj_objectWithKeyValues:dict];
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];
    [self endRefresh];
//    [SVProgressHUD dismiss];
}
- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)blockBannerIndex {
    WeakStament(ws);
    self.headerView.indexBlock = ^(NSInteger indexNum, NFourIconModel *model) {
        YYLog(@"%ld",(long)indexNum);
        switch (indexNum) {
            case 0:
            {
                ws.tabBarController.selectedIndex = 1;
            }
                break;
            case 1:
            {
                ws.tabBarController.selectedIndex = 1;
                //滚动到相应位置
//                [[NSNotificationCenter defaultCenter]postNotificationName:@"scroll" object:nil];
                
            }
                break;
            case 2:
            {
//                [AFToast showText:@"开发中……"];
                 ws.tabBarController.selectedIndex = 1;
            }
                break;
            case 3:
            {
//                [AFToast showText:@"开发中……"];
                 ws.tabBarController.selectedIndex = 1;
            }
                break;
                
            default:
                break;
        }
    };
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NFeedbackModel *model = self.dataArray[indexPath.row];
    NProblemContentViewController *proc = [NProblemContentViewController new];
    proc.hidesBottomBarWhenPushed = YES;
    proc.problemID = [NSString stringWithFormat:@"%ld",(long)model.questionId];
    proc.pushType = NProblemPushTypeCollection;
    [self.navigationController pushViewController:proc animated:YES];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    view.backgroundColor = [UIColor magentaColor];
    NHomeSectionHeader *header = [[[NSBundle mainBundle] loadNibNamed:@"NHomeSectionHeader" owner:self options:nil] lastObject];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [header.moreLabel addGestureRecognizer:tap];
    header.frame = CGRectMake(0, 0, ScreenWidth, 40);
    [view addSubview:header];
    return view;
    
}

//手势点击事件

- (void)tapAction {
    
    NMyFeedbackViewController *mf = [NMyFeedbackViewController new];
    mf.flag = @"open";
    mf.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mf animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.f;
}

#pragma mark - datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NFeedbackCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NFeedbackModel *model = self.dataArray[indexPath.row];
    if (self.dataArray.count>indexPath.row) {
        [cell configWithData:model];
        YYLog(@"%@",cell.model);
    }
    return cell;
}

@end
